﻿//ctrl +A 全选   ctrl+K   ctrl+F 格式化代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace 等高线2
{
    struct Edge
    {
        public int rate;
        public bool isPoint;
    }
    public partial class Form1 : Form
    {
        private double[,] p;
        private double[,] pp = new double[79, 119];
        private Color col = Color.FromArgb(100, 100, 100);
        private bool draw = false;
        private bool draw2 = false;
        private bool draw3 = false;
        private double height;
        private List<List<Point>> LP = new List<List<Point>>();
        private List<Point> lp = new List<Point>();

        private Edge[,] xSide = new Edge[79, 118];
        private Edge[,] ySide = new Edge[78, 119];

        public Form1()
        {
            InitializeComponent();
        }

        private void change(double[,] a)
        {
            for (int i = 0; i < 79; i++)
            {
                for (int j = 0; j < 119; j++)
                {
                    pp[i, j] = Math.Round(p[i, j], 1, MidpointRounding.AwayFromZero);
                    if(pp[i,j]==150.0)
                    {
                        if (p[i, j] > 150)
                            pp[i, j] = 150.1;
                        else if (p[i, j] < 150)
                            pp[i, j] = 149.9;
                    }
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("C:\\Users\\711\\Desktop\\4-2bdep.grd");
            sr.ReadLine();
            string s = sr.ReadLine().Trim();
            string[] ss = s.Split(new char[] { ' ' });
            p = new double[79, 119];
            sr.ReadLine(); sr.ReadLine(); sr.ReadLine();
            int i, j, z; int k = 12;
            for (i = 0; i < 79; i++)
            {
                for (j = 0; j < k; j++)
                {
                    s = sr.ReadLine();
                    ss = s.Split(new char[] { ' ' });
                    if (j != k - 1)
                        for (z = 0; z < 10; z++)
                        {
                            p[i, j * 10 + z] = Double.Parse(ss[z]);
                        }
                    else
                    {
                        for (z = 0; z < 9; z++)
                            p[i, 110 + z] = Double.Parse(ss[z]);
                    }
                }
                sr.ReadLine();
            }
            sr.Close();
            change(p);
            pictureBox1.Location = new Point(20, 20);
            this.pictureBox1.Width = 1000;
            this.pictureBox1.Height = 780;
        }

        private void paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(pictureBox1.BackColor);
            #region
            //             for(int i=0;i<119;i++)
            //             {
            //                 g.DrawLine(new Pen(new SolidBrush(col)), new Point(i * 8, 0), new Point(i * 8, 640));
            //             }
            //             for (int i=0;i<79;i++)
            //             {
            //                 g.DrawLine(new Pen(new SolidBrush(col)), new Point(0, i*8), new Point(960, i*8));
            //             }
            #endregion
            if (draw)
            {
                // draw = false;
                for (int i = 0; i < 79; i++)
                {
                    for (int j = 0; j < 119; j++)
                    {
                        if (pp[i, j] > height)
                            g.FillEllipse((new SolidBrush(Color.Red)), j * 8 - 1, i * 8 - 1, 2, 2);
                        else
                            g.FillEllipse((new SolidBrush(Color.Blue)), j * 8 - 1, i * 8 - 1, 2, 2);
                    }
                }
            }

            if (draw3)
            {
                g.DrawCurve(new Pen(new SolidBrush(Color.Black)), lp.ToArray());
                //g.DrawCurve()
            }
           // draw2 = false;
            if (draw2)
            {
                for (int i = 0; i < 79; i++)
                {
                    for (int j = 0; j < 118; j++)
                    {
                        if (xSide[i, j].isPoint == true)
                        {
                            g.FillEllipse(new SolidBrush(Color.Black), j * 8 + xSide[i, j].rate - 2, i * 8 - 2, 4, 4);
                        }
                    }
                }
                for (int i = 0; i < 78; i++)
                {
                    for (int j = 0; j < 119; j++)
                    {
                        if (ySide[i, j].isPoint == true)
                        {
                            g.FillEllipse(new SolidBrush(Color.Black), j * 8 - 2, i * 8 + ySide[i, j].rate - 2, 4, 4);
                        }
                    }
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Empty.");
                return;
            }
            height = Double.Parse(textBox1.Text);
            draw = true;
            pictureBox1.Invalidate();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            double a, b, c;
            draw2 = true;
            for (int i = 0; i < 79; i++)
            {
                for (int j = 0; j < 118; j++)
                {
                    a = pp[i, j] - height;
                    b = pp[i, j + 1] - height;
                    if ((a > 0 && b < 0) || (a < 0 && b > 0))
                    {
                        xSide[i, j].isPoint = true;
                        a = Math.Abs(a); b = Math.Abs(b);
                        c = a / (a + b);
                        c = c / 0.125;
                        xSide[i, j].rate = (int)c + 1;
                        if (xSide[i, j].rate > 7)
                            xSide[i, j].rate = 7;
                    }
                    else
                        xSide[i, j].isPoint = false;
                }
            }
            for (int i = 0; i < 78; i++)
            {
                for (int j = 0; j < 119; j++)
                {
                    a = pp[i, j] - height;
                    b = pp[i + 1, j] - height;
                    if ((a > 0 && b < 0) || (a < 0 && b > 0))
                    {
                        a = Math.Abs(a); b = Math.Abs(b);
                        c = a / (a + b);
                        c = c / 0.125;
                        ySide[i, j].rate = (int)c + 1;
                        ySide[i, j].isPoint = true;
                        if (ySide[i, j].rate > 7)
                            ySide[i, j].rate = 7;
                    }
                    else
                        ySide[i, j].isPoint = false;
                }
            }
            pictureBox1.Invalidate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 118; i++)
            {
                if (xSide[78, i].isPoint == true)
                {
                    push(78, i, 2);
                    pictureBox1.Invalidate();
                    
                }
            }
            for (int i = 0; i < 118; i++)
            {
                if (xSide[0, i].isPoint == true)
                {
                    push(0, i, 1);
                    pictureBox1.Invalidate();

                }
            }
        }

        private int count = 0;
        private void push(int i, int j, int t)
        {
            int a = i; int b = j; int type = t;
            lp.Add(new Point(b * 8 + xSide[a, b].rate,a * 8));
            xSide[a, b].isPoint = false;
            while (count < 500)
            {
                textBox2.Text += a.ToString() + "  " + b.ToString() + "  " + type.ToString() + "\r";

                #region type 1
                if (type == 1)
                {
                    count++;
                    if (xSide[a + 1, b].isPoint == true)
                    {
                        a = a + 1;
                        lp.Add(new Point(b * 8 + xSide[a, b].rate,a * 8));
                        xSide[a, b].isPoint = false;
                        type = 1;
                        if (a == 118)
                        {
                            goto finish;
                        }
                    }
                    else if (ySide[a, b].isPoint == true)
                    {
                        type = 4;
                        lp.Add(new Point(b * 8, a * 8 + ySide[a, b].rate));
                        ySide[a, b].isPoint = false;
                        if (b == 0)
                        {
                            goto finish;
                        }
                    }
                    else if (ySide[a, b + 1].isPoint == true)
                    {
                        type = 3;
                        b = b + 1;
                        lp.Add(new Point(b * 8, a * 8 + ySide[a, b].rate));
                        ySide[a, b].isPoint = false;
                        if (b == 118)
                            goto finish;
                    }
                }
                #endregion
                #region type 2
                else if (type == 2)
                {
                    count++;
                    if (xSide[a - 1, b].isPoint == true)
                    {
                        type = 2;
                        a = a - 1;
                        lp.Add(new Point(b * 8 + xSide[a, b].rate, a * 8));
                        xSide[a, b].isPoint = false;
                        if (a <= 0)
                            goto finish;
                    }
                    else if (ySide[a - 1, b].isPoint == true)
                    {
                        type = 4;
                        a = a - 1;
                        lp.Add(new Point(b * 8, a * 8 + ySide[a, b].rate));
                        ySide[a, b].isPoint = false;
                        if (b <= 0)
                            goto finish;
                    }
                    else if (ySide[a - 1, b + 1].isPoint == true)
                    {
                        type = 3;
                        a = a - 1; b = b + 1;
                        lp.Add(new Point(b * 8,a * 8 + ySide[a, b].rate));
                        ySide[a, b].isPoint = false;
                        if (b >= 118)
                            goto finish;
                    }
                }
                #endregion
                #region type 3
                else if (type == 3)
                {
                    count++;
                    if (ySide[a, b + 1].isPoint == true)
                    {
                        type = 3;
                        b = b + 1;
                        lp.Add(new Point(b * 8,a * 8 + ySide[a, b].rate));
                        ySide[a, b].isPoint = false;
                        if (b >= 118)
                            goto finish;
                    }
                    else if (xSide[a, b].isPoint == true)
                    {
                        type = 2;
                        lp.Add(new Point(b * 8 + xSide[a, b].rate, a * 8));
                        xSide[a, b].isPoint = false;
                        if (a <= 0)
                            goto finish;
                    }
                    else if (xSide[a + 1, b].isPoint == true)
                    {
                        type = 1;
                        a = a + 1;
                        lp.Add(new Point(b * 8 + xSide[a, b].rate, a * 8));
                        xSide[a, b].isPoint = false;
                        if (b >= 118)
                            goto finish;
                    }
                }
                #endregion
                #region type 4
                else if (type == 4)
                {
                    count++;
                    if (ySide[a, b - 1].isPoint == true)
                    {
                        b = b - 1;
                        type = 4;
                        lp.Add(new Point(b*8,a * 8 + ySide[a, b].rate));
                        ySide[a, b].isPoint = false;
                        if (b <= 0)
                            goto finish;
                    }
                    else if (xSide[a, b - 1].isPoint == true)
                    {
                        b = b - 1;
                        type = 2;
                        lp.Add(new Point(b * 8 + xSide[a, b].rate, a * 8));
                        xSide[a, b].isPoint = false;
                        if (a <= 0)
                            goto finish;
                    }
                    else if (xSide[a + 1, b - 1].isPoint == true)
                    {
                        a = a + 1;
                        b = b - 1;
                        type = 1;
                        lp.Add(new Point(b * 8 + xSide[a, b].rate,a * 8));
                        xSide[a, b].isPoint = false;
                        if (b >= 118)
                            goto finish;
                    }
                }
                #endregion

            }

        finish:
            if (lp.Count > 1)
                draw3 = true;

        }



    }
}
